/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.Flow;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.FlowRepository;
import me.zhengjie.modules.system.service.FlowService;
import me.zhengjie.modules.system.service.dto.FlowDto;
import me.zhengjie.modules.system.service.dto.FlowQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.FlowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author zmm
* @date 2020-06-26
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FlowServiceImpl implements FlowService {

    private final FlowRepository flowRepository;
    private final FlowMapper flowMapper;

    @Override
    public Map<String,Object> queryAll(FlowQueryCriteria criteria, Pageable pageable){
        Page<Flow> page = flowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(flowMapper::toDto));
    }

    @Override
    public List<FlowDto> queryAll(FlowQueryCriteria criteria){
        return flowMapper.toDto(flowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public FlowDto findById(Long id) {
        Flow flow = flowRepository.findById(id).orElseGet(Flow::new);
        ValidationUtil.isNull(flow.getId(),"Flow","id",id);
        return flowMapper.toDto(flow);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FlowDto create(Flow resources) {
        return flowMapper.toDto(flowRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Flow resources) {
        Flow flow = flowRepository.findById(resources.getId()).orElseGet(Flow::new);
        ValidationUtil.isNull( flow.getId(),"Flow","id",resources.getId());
        flow.copy(resources);
        flowRepository.save(flow);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            flowRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FlowDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FlowDto flow : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", flow.getCreateTime());
            map.put("更新时间", flow.getUpdateTime());
            map.put("创建者", flow.getCreateBy());
            map.put("更新者", flow.getUpdateBy());
            map.put(" name",  flow.getName());
            map.put("所属公司", flow.getCid());
            map.put("所属项目", flow.getPid());
            map.put("流程类型", flow.getFlowType());
            map.put("模版状态", flow.getStatus());
            map.put("申请次数", flow.getApplyCount());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}